//
//  Info4ViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/4/25.
//

#import "Info4ViewController.h"
#import "Info4CollectionViewCell.h"
#import "ViewController.h"
@interface Info4ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic , strong)InfoModel * seletedModel;

@end

@implementation Info4ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.zx_navTitle = SQNSLocalString(@"兴趣标签");
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"me背景");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.collectionView registerClass:[Info4CollectionViewCell class] forCellWithReuseIdentifier:@"Info4CollectionViewCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    HeaderView * xiayibu = [[HeaderView alloc]init];
    xiayibu.image = @"按钮背景";
    xiayibu.text = SQNSLocalString(@"提交");
    xiayibu.colorString = @"#FFFFFF";
    [xiayibu.btn addTarget:self action:@selector(SQ_tijiaoButton) forControlEvents:UIControlEventTouchUpInside];
    xiayibu.font = FONT_MEDIUM(18);
    [self.view addSubview:xiayibu];
    [xiayibu mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-110);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight);
    }];
    if (!self.isEdit) {
        [self SQ_collectViewAddRefresh];
        self.zx_navLeftBtn.hidden = YES;
    }
    [self SQ_Header];
    
}
-(void)SQ_tijiaoButton{
    NSMutableArray * preference = [NSMutableArray new];
    for (InfoModel * model in self.jsonArray) {
        if (model.isSeleted) {
            [preference addObject:model.name];
        }
    }
    if (preference.count <= 0) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请选择您的兴趣标签")];
        return;
    }
    if (preference.count < 3) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"兴趣标签不得少于3个")];
        return;
    }
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[preference componentsJoinedByString:@","] forKey:@"preference"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (self.isEdit) {
            [self SQ_popViewVC];
        } else {
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init] ];
             self.view.window.rootViewController = nav;
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:@"preference/list" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSArray * array = [[NSArray alloc]initWithArray:httpModel.data];
        if (array.count >0) {
            [self.jsonArray removeAllObjects];
            NSDictionary * data = [array firstObject];
            NSArray * moarray = [InfoModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"list"]];
            [self.jsonArray addObjectsFromArray:moarray];
        }
        if (self.isEdit&&[self.rigntString isNotEmpty]) {
            NSArray * seleted = [self.rigntString componentsSeparatedByString:@","];
            for (InfoModel * model in self.jsonArray) {
                for (NSString * name in seleted) {
                    if ([name isEqualToString:model.name]) {
                        model.isSeleted = YES;
                    }
                }
            }
        }
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    [self.collectionView.mj_footer endRefreshing];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    Info4CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Info4CollectionViewCell" forIndexPath:indexPath];
      cell.model = [self.jsonArray objectAtIndex:indexPath.item];
     return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return CGSizeMake((SCREEN_WIDETH-40)/3.f-1, 60);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    InfoModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    model.isSeleted = !model.isSeleted;
    [self.collectionView reloadData];
}

@end
